<?php
class AppUserAction extends Action {
	/**登陆 **/
	public function login(){
		/* if (!$this->isPost()){
		 $result['code']=0;
		 $result['msg']="请求错误";
		 $this->ajaxReturn($result,'JSON');
		 }*/
		$user = M("User");
		$phone=$_POST['phone'];
		$where['phone'] = $phone;
		$where['password'] = $_POST['password'];
		$where['type'] = '0';
		$adminResult = $user->where($where)->find();
		//dump($adminResult);
		if(empty($adminResult))
		{
			$result['code']=5;
			$result['msg']="用户名或密码错误！";
		}
		else
		{
			$result['code']=1;
			$result['msg']="success";
			$result['user']=$adminResult;
			$_SESSION['user'] = $adminResult;
		}
		//        echo var_dump($result);
		//        $this->display();
		$this->ajaxReturn($result,'JSON');
	}
	/**退出登陆 **/
	public function logout(){
		unset($_SESSION["user"]);
		$result['code']=1;
		$result['msg']="success";
		$this->ajaxReturn($result,'JSON');
	}
	/**注册 **/
	public function register(){
		if (!$this->isPost()){
			$result['code']='0';
			$result['msg']="请求错误";
			$this->ajaxReturn($result,'JSON');
		}
		if (empty($_POST['name'])){
			$result['code']='2';
			$result['msg']="请输入昵称";
		}elseif (empty($_POST['phone'])){
			$result['code']='3';
			$result['msg']="请输入手机号码";
		}elseif (empty($_POST['password'])){
			$result['code']='4';
			$result['msg']="请输入密码";
		}elseif (empty($_POST['repassword'])){
			$result['code']='5';
			$result['msg']="请输入密码确认";
		}
		if ($_POST['password']!=$_POST['repassword'])
		{
			$result['code']='6';
			$result['msg']="两次输入的密码不一样";
		}
		$user=M("User");
		$data['phone']=$_POST['phone'];
		$rs = $user->where($data)->select();
		/*
		 *  验证手机号码是否注册过
		 */
		if(!empty($rs))
		{
			$result['code']='7';
			$result['msg']="手机号码已经注册";
			$this->ajaxReturn($result,'JSON');
		}
		$data['name']=$_POST['name'];
		$data['username']=$_POST['name'];
		$data['password']=$_POST['password'];
		$data['create_time']=date('Y-m-d H:i',time());
		/*
		 * 插入注册数据
		 */
		$r=$user->add($data);
		if ($r==false)
		{
			$result['code']='8';
			$result['msg']="插入错误";
		}else{
			$result['code']=1;
			$result['msg']="注册成功";
			$user=M('User');
			$user=$user->find($r);
			$result['user']=$user;
		}
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 个人中心
	 */
	/*public function personalCenter(){
		$result['user']=$_SESSION['user'];
		$this->ajaxReturn($result,'JSON');
		}*/
	/**
	 * 重置密码
	 **/
	public function resetPassword()
	{
		$user=M('User');
		$where['id']=$_POST['user_id'];
		//		$us = $user->where($where)->select();
		//
		//		if(empty($us)){
		//			$result['code']=1;
		//			$result['msg']="用户名不存在";
		//			$this->ajaxReturn($result,'JSON');
		//		}
		$where['password']=$_POST['oldpassword'];
		$where['type']='0';
		$us = $user->where($where)->select();
		if(empty($us)){
			$result['code']=2;
			$result['msg']="用户名或原密码不正确";
			$this->ajaxReturn($result,'JSON');
		}
		if($_POST['newpassword']!=$_POST['repassword'])
		{
			$result['code']=3;
			$result['msg']="两次输入的密码不同";
			$this->ajaxReturn($result,'JSON');
		}
		$user->password=$_POST['newpassword'];
		$wheres['id']=$_POST['user_id'];
		$user->where($wheres)->save();
		$result['code']=0;
		$result['msg']="密码修改成功";
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 修改头像
	 */
	public function resetImage()
	{
		$base64=$_POST['files'];
		$where['id']=$_POST['id'];
		$user = M('User');
		$arr_str=explode(",",$base64);//以空格为拆分条件
		if(!empty($arr_str))
		{
			$s = base64_decode($arr_str[1]);//输出aaaa
	//		$s = base64_decode(str_replace('data:image/jpeg;base64,', '', $base64));
			$uniqid = 'user'.time();
			$image = file_put_contents('./Public/Uploads/image/user/'.$uniqid.'.png', $s);
			$data['image'] = $uniqid.'.png';
			$user->data($data)->where($where)->save();
			$result['msg']="头像修改成功";
			$result['image']=$uniqid.'.png';
			$this->ajaxReturn($result,'JSON');
		}else{
			$result['msg']="头像修改失败";
			$this->ajaxReturn($result,'JSON');
		}
	}
/*	public function resetImage()
	{
		//导入图片上传类
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->saveRule = "touxiang";//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
		$upload->savePath =  './Public/images/login/';// 设置附件上传目录
		//$upload->thumb=true;//是否开启图片文件缩略
		//$upload->thumbMaxWidth='400,100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		//$upload->thumbMaxHeight='400,100';
		//$upload->thumbPrefix='s_,m_';  //缩略图文件前缀
		//$upload->thumbSuffix='-s,-m';//后缀
		//在数据库当中也存在一个文件名即可
		//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
		if(!$upload->upload()) {// 上传错误提示错误信息
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		// 保存表单数据 包括附件数据
		$game = M("User");
		//$game->create();
		$data['image'] = $info[0]['savename'];
		$where['id'] = $_POST['id'];
		$rs = $game->data($data)->where($where)->save();
		$image = $info[0]['savepath'] . $info[0]['savename'];
		if($rs!=false){
			$result['code']=1;
			$result['msg']='success';
			//$result['image']=$image;
			$this->ajaxReturn($result,'JSON');
		}else{
			$result['code']=2;
			$result['msg']='updata error';
			$this->ajaxReturn($result,'JSON');
		}
	}*/
	function homeHome(){
		$home = M('Home');
		$where['home'] = '1';
		$homes = $home -> field('h.image')->join('left join s_home_image h on h.home_id = s_home.id')->where($where)->order('h.id asc')->select();
		$result['homes']=$homes;
		$this->ajaxReturn($result,'JSON');
	}
}
?>